﻿//-----------------------------------------------------------------
//
// @(#)$Id: NoCompleteCustomLableHander.aspx.cs,v 1.0 2013/12/13 17:55:40 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 17:55:40 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ebay.Dal;

public partial class UI_Orders_NoCompleteCustomLableHander : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string orderId = Request["orderId"];
            string ebayUserId = Request["ebayUserId"];

            if (!string.IsNullOrEmpty(orderId) && !string.IsNullOrEmpty(ebayUserId))
            {
                Bind(orderId, ebayUserId);
                txtEbayUserId.Value = ebayUserId;
                txtOrderId.Value = orderId;
            }
        }
    }
    private void Bind(string orderId, string ebayUserId)
    {
        Orders o = new Orders();
        DataTable table = o.GetNoCompleteCustomLable(ebayUserId, orderId);
        if (table.Rows.Count > 0)
        {
            this.repParent.DataSource = table;
            this.repParent.DataBind();
        }
        else
        {
            divMain.Style.Add("line-height", "330px");
            divMain.Style.Add("text-align", "center");
            divMain.Style.Remove("overflow-X");
            divMain.Style.Remove("overflow");
            divMain.InnerHtml = "<h1 style = \"color:red;\">没有需要处理的订单</h1>";
        }
    }
    protected void rptypelist_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Orders o = new Orders();
            Repeater childRep = e.Item.FindControl("repChild") as Repeater;
            DataRowView rowv = (DataRowView)e.Item.DataItem;
            string parentId = rowv["ProductId"].ToString();

            //读取不完全代码资料
            DataTable table = o.GetProductById2(parentId);

            if (table.Rows.Count > 0)
            {
                string lsumCQty = rowv["sQty"].ToString();
                //发货总数量
                int sendTotalQty = Convert.ToInt32(lsumCQty);

                //计算用到的总数的副本
                int cloneTotalQty = sendTotalQty;

                //计算可用库存总数量
                object objsumFQty = table.Compute("Sum(entyQty)", "entyQty > 0");
                int sumFQty = objsumFQty == DBNull.Value ? 0 : Convert.ToInt32(objsumFQty);

                //如果可用库存>0并且可用库存够发货数量 才计算随即数,否则人为选择
                if (sumFQty > 0 && sendTotalQty <= sumFQty)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        //单条商品库存量
                        int stockQty = Convert.ToInt32(table.Rows[i]["entyQty"]);
                        if (stockQty <= 0)
                        {
                            continue;
                        }
                        //占用总库存比率
                        decimal percent = (decimal)stockQty / sumFQty;

                        //计算发货数量 去掉小数位
                        int cQty = (int)Math.Ceiling((decimal)sendTotalQty * percent);

                        //如果计算出的发货数量大于库存量,取库存量
                        if (cQty > stockQty)
                        {
                            cQty = stockQty;
                        }

                        //减去本条商品发货数量 加条件判断,排除计算错误的情况
                        if (cloneTotalQty >= cQty)
                        {
                            cloneTotalQty -= cQty;
                        }
                        else
                        {
                            cQty = cloneTotalQty;
                            cloneTotalQty = 0;
                        }

                        //选中本条商品并且更新发货数量
                        table.Rows[i]["cQty"] = cQty;
                        table.Rows[i]["chk"] = "true";

                        //发货数量计算完毕,退出
                        if (cloneTotalQty <= 0)
                            break;
                    }
                }
            }
            childRep.DataSource = table;
            childRep.DataBind();
        }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (divMain.Controls[0] is System.Web.UI.LiteralControl)
        {
            if (Session["UserId"] == null)
            {
                WebUtility.ShowMsg("登陆超时,请重新登陆", this);
                return;
            }
            string Muser = Session["UserId"].ToString();
            DateTime Mtime = DateTime.Now;
            List<ebay.Model.CustomerResolve> list = new List<ebay.Model.CustomerResolve>();
            for (int i = 0; i < repParent.Items.Count; i++)
            {
                string lsQty = (repParent.Items[i].FindControl("sQty") as HtmlTableCell).InnerText;
                string ProductId = (repParent.Items[i].FindControl("ProductId") as HtmlTableCell).InnerText;
                int isQty = Convert.ToInt32(lsQty);

                Repeater repc = repParent.Items[i].FindControl("repChild") as Repeater;
                int icQty = 0;
                if (repc != null)
                {
                    for (int j = 0; j < repc.Items.Count; j++)
                    {

                        CheckBox chkBox = repc.Items[j].FindControl("chkitem") as CheckBox;
                        bool select = chkBox.Checked;
                        if (select)
                        {
                            string lcQty = (repc.Items[j].FindControl("cQty") as HtmlInputText).Value;
                            string lQty = (repc.Items[j].FindControl("Qty") as HtmlTableCell).InnerText;
                            string lPrice = (repc.Items[j].FindControl("FOrderPrice") as HtmlTableCell).InnerText;
                            int iicQty = Convert.ToInt32(lcQty);
                            int iQty = Convert.ToInt32(lQty);
                            decimal price = Convert.ToDecimal(lPrice);
                            icQty += iicQty;
                            ebay.Model.CustomerResolve m = new ebay.Model.CustomerResolve();
                            m.ActualPostage = 0m;
                            m.Atime = new DateTime(1900, 1, 1);
                            m.Auser = "";
                            m.CustomerLable = (repParent.Items[i].FindControl("CustomLable") as HtmlTableCell).InnerText;
                            m.EbayUserId = txtEbayUserId.Value;
                            m.ItemCost = price;
                            m.MNote = "";
                            m.Mtime = Mtime;
                            m.Muser = Muser;
                            m.OrderLineItemId = (repParent.Items[i].FindControl("OrderLineitemId") as HtmlTableCell).InnerText;
                            m.PackageFee = 0m;
                            m.PayFee = 0m;
                            m.ProductId = (repc.Items[j].FindControl("ProductId2") as HtmlTableCell).InnerText;
                            m.Quantity = iicQty * iQty;
                            m.Rate = 1m;
                            m.RefundLoss = 0m;
                            m.SalePrice = price;
                            m.TheoryPostAge = 0m;
                            m.TransactionFees = 0m;
                            m.WarehouseId = Convert.ToInt32((repc.Items[j].FindControl("FStockId") as HtmlTableCell).InnerText);
                            list.Add(m);
                        }
                    }
                    if (isQty != icQty)
                    {
                        WebUtility.ShowMsg("产品：" + ProductId + "选择数量和总数量不符,请修正后在操作", this);
                        return;
                    }
                }
            }
            ebay.Dal.CustomerResolve c = new CustomerResolve();
            c.AddList(list, txtEbayUserId.Value, txtOrderId.Value);
            Response.Write("<script>window.close();</script>");
        }
        else
        {
            Response.Write("<script>window.close();</script>");
        }
    }
}
